package com.example.dessert.util;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

/**
 * @ClassName: ResponsePojo
 * @Description:
 * @Author
 * @Date 2022/1/4
 * @Version 1.0
 */
public class ResponsePojo {
    // 定义jackson对象
    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 响应业务状态
     * 200 正常返回
     * 201 参数错误
     * 403 服务器禁止-参数错误之类原因
     * 408 请求超时-服务器长时间无响应
     * 500 服务器内部错误
     * 501 服务器不支持当前请求
     * 502 调用远程网络不成功-常见网关错误
     */
    private Integer code;

    // 响应消息
    private String message;

    // 响应中的数据
    private Object data;



    public static ResponsePojo build(Integer code, String message, Object data) {
        return new ResponsePojo(code, message, data);
    }

    public static ResponsePojo ok(Object data) {
        return new ResponsePojo(data);
    }

    public static ResponsePojo ok() {
        return new ResponsePojo(null);
    }

    public static ResponsePojo error(Integer code, String message) {
        return new ResponsePojo(code, message, null);
    }

    public ResponsePojo() {

    }

    public static ResponsePojo build(Integer code, String message) {
        return new ResponsePojo(code, message, null);
    }

    public ResponsePojo(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResponsePojo(Object data) {
        this.code = ResultCode.SUCCESS_CODE;
        this.message = "成功";
        this.data = data;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }



    /**
     * 将json结果集转化为ResponsePojo对象
     *
     * @param jsonData json数据
     * @param clazz ResponsePojo中的object类型
     * @return
     */
    public static ResponsePojo formatToPojo(String jsonData, Class<?> clazz) {
        try {
            if (clazz == null) {
                return MAPPER.readValue(jsonData, ResponsePojo.class);
            }
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (clazz != null) {
                if (data.isObject()) {
                    obj = MAPPER.readValue(data.traverse(), clazz);
                } else if (data.isTextual()) {
                    obj = MAPPER.readValue(data.asText(), clazz);
                }
            }
            return build(jsonNode.get("code").intValue(), jsonNode.get("message").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 没有object对象的转化
     *
     * @param json
     * @return
     */
    public static ResponsePojo format(String json) {
        try {
            return MAPPER.readValue(json, ResponsePojo.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Object是集合转化
     *
     * @param jsonData json数据
     * @param clazz 集合中的类型
     * @return
     */
    public static ResponsePojo formatToList(String jsonData, Class<?> clazz) {
        try {
            JsonNode jsonNode = MAPPER.readTree(jsonData);
            JsonNode data = jsonNode.get("data");
            Object obj = null;
            if (data.isArray() && data.size() > 0) {
                obj = MAPPER.readValue(data.traverse(),
                        MAPPER.getTypeFactory().constructCollectionType(List.class, clazz));
            }
            return build(jsonNode.get("code").intValue(), jsonNode.get("message").asText(), obj);
        } catch (Exception e) {
            return null;
        }
    }

    @Override
    public String toString() {
        return "ResponseObj{" +
                ", message='" + message + '\'' +
                ", data=" + data +
                ",code='" + code + '\'' +
                '}';
    }
}
